﻿using RXY_Mall.Application.Goods;
using RXY_Mall.Application.Goods.Dto;
using RXY_Mall.Application.SystemSetting;
using RXY_Mall.Application.SystemSetting.Dto;
using RXY_Mall.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace RXY_Mall.Admin.Controllers
{
    public class SystemSettingController : BaseController
    {
        private IGoodsService _goodsService;
        private ISystemSettingService _systemSettingService;
        public SystemSettingController()
        {
            _goodsService = new GoodsService();
            _systemSettingService = new SystemSettingService();
        }

        #region 品牌
        public ActionResult BrandList()
        {
            return View();
        }

        public JsonResult GetBrandsByPage(int iDisplayStart, int iDisplayLength, string brand_name, int brand_status)
        {
            var result = _goodsService.GetBrandsByPage( brand_name, brand_status, iDisplayStart / iDisplayLength + 1, iDisplayLength);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
            }
            return Json(new { iTotalRecords = result.data.TotalCount, iTotalDisplayRecords = result.data.TotalCount, aaData = result.data }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 添加分类
        /// </summary>
        /// <param name="id">父ID</param>
        /// <param name="tid">类别</param>
        /// <returns></returns>
        public ActionResult AddBrand()
        {
            return View();
        }

        public ActionResult EditBrand(int id = 0)
        {
            ViewBag.id = id;
            return View();
        }

        [HttpGet]
        public JsonResult GoodsBrandInfo(int id = 0)
        {
            if (id > 0)
            {
                var model = _goodsService.GetCategoryById(id);
                return Json(model, JsonRequestBehavior.AllowGet);
            }
            return null;
        }

        [HttpPost]
        public async Task<JsonResult> SaveBrand(CategoryInput input)
        {
            var result = await _goodsService.AddorEditCategory(MemberId, input);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = "保存成功。", sId = result.data });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        }

        #endregion

        #region 商品分类
        /// <summary>
        /// 商品分类
        /// </summary>
        /// <param name="id">父类别</param>
        /// <param name="tid">类型1,2,3</param>
        /// <returns></returns>
        public ActionResult Category(int id = 1)
        {
            ViewBag.tid = id;
            //ViewBag.tName = "普通";
            //switch (id)
            //{
            //    case 2:
            //        ViewBag.tName = "合力免单";
            //        break;
            //    case 3:
            //        ViewBag.tName = "趣卡专享";
            //        break;
            //}
            return View();
        }

        public JsonResult CategoryList(string sEcho, int iDisplayStart, int iDisplayLength, string dateMin, string dateMax, string title, int parentId, int type = 1)
        {
            int totalCount = 0;
            var list = _goodsService.GetAllCategoryByPage(iDisplayStart, iDisplayLength, dateMin, dateMax, title, parentId, type, out totalCount);
            return Json(new { sEcho = sEcho, iTotalRecords = totalCount, iTotalDisplayRecords = totalCount, aaData = list }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 添加分类
        /// </summary>
        /// <param name="id">父ID</param>
        /// <param name="tid">类别</param>
        /// <returns></returns>
        public ActionResult AddCategory(int id = -1, int tid = 0)
        {
            ViewBag.id = id;
            ViewBag.tid = tid;
            return View();
        }

        public ActionResult EditCategory(int id = 0)
        {
            ViewBag.id = id;
            return View();
        }

        [HttpPost]
        public JsonResult GetCategoryTree(int id = 0)
        {
            var list = _goodsService.GetCategoryTree(id);
            return Json(list);
        }

        [HttpGet]
        public JsonResult GoodsCategoryInfo(int id = 0)
        {
            if (id > 0)
            {
                var model = _goodsService.GetCategoryById(id);
                return Json(model, JsonRequestBehavior.AllowGet);
            }
            return null;
        }

        [HttpPost]
        public async Task<JsonResult> SaveCategory(CategoryInput input)
        {
            var result = await _goodsService.AddorEditCategory(MemberId, input);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = "保存成功。", sId = result.data });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        }

        //禁用/启用类别
        [HttpPost]
        public ActionResult SetCategoryIsUse()
        {
            string id = Request.Form["id"];
            string isUse = Request.Form["isUse"];
            int idDefault = 0;
            int isUseDefault = 0;
            if (string.IsNullOrWhiteSpace(id) || !int.TryParse(id, out idDefault))
            {
                return Json(new { status = "n", info = "ID错误" });
            }
            if (string.IsNullOrWhiteSpace(isUse) || !int.TryParse(isUse, out isUseDefault))
            {
                return Json(new { status = "n", info = "参数错误" });
            }

            var result = _goodsService.SetCategoryIsUse(MemberId, idDefault, isUseDefault);
            if (result)
            {
                return Json(new { status = "y", info = "操作成功。" });
            }
            else
            {
                return Json(new { status = "n", info = "操作失败。" });
            }
        }
        #endregion

        #region 商品服务
        public ActionResult Service()
        {
            return View();
        }

        public ActionResult AddService(int id = 0)
        {
            ViewBag.id = id;
            ViewBag.text = id == 0 ? "添加" : "修改";
            return View();
        }
        [HttpPost]
        public async Task<JsonResult> AddorEditService(ServiceInput input)
        {
            if (input.id == 0 && _systemSettingService.IsExistService(input.title))
            {
                return Json(new { status = "n", info = "该服务名称已存在！" });
            }
            else
            {
                var result = await _systemSettingService.AddorEditService(MemberId, input);
                if (result.code == 1)
                {
                    return Json(new { status = "y", info = "保存成功。" });
                }
                else
                {
                    return Json(new { status = "n", info = result.msg });
                }
            }
        }

        [HttpGet]
        public JsonResult ServiceInfo(int id = 0)
        {
            if (id > 0)
            {
                var model = _systemSettingService.GetServiceById(id);
                return Json(model, JsonRequestBehavior.AllowGet);
            }
            return null;
        }

        public JsonResult ServiceList(string sEcho, int iDisplayStart, int iDisplayLength, string name, int status)
        {
            int totalCount = 0;
            var list = _systemSettingService.GetAllServiceByPage(iDisplayStart, iDisplayLength, name, status, out totalCount);
            return Json(new { sEcho = sEcho, iTotalRecords = totalCount, iTotalDisplayRecords = totalCount, aaData = list }, JsonRequestBehavior.AllowGet);
        }

        //禁用/启用服务
        [HttpPost]
        public ActionResult SetServiceIsUse()
        {
            string id = Request.Form["id"];
            string isUse = Request.Form["isUse"];
            int idDefault = 0;
            int isUseDefault = 0;
            if (string.IsNullOrWhiteSpace(id) || !int.TryParse(id, out idDefault))
            {
                return Json(new { status = "n", info = "ID错误" });
            }
            if (string.IsNullOrWhiteSpace(isUse) || !int.TryParse(isUse, out isUseDefault))
            {
                return Json(new { status = "n", info = "参数错误" });
            }

            var result = _systemSettingService.SetIsServiceUse(MemberId, idDefault, isUseDefault);
            if (result)
            {
                return Json(new { status = "y", info = "操作成功。" });
            }
            else
            {
                return Json(new { status = "n", info = "操作失败。" });
            }
        }
        #endregion

        #region 会员权益
        public ActionResult UserRule()
        {
            return View();
        }

        public ActionResult AddUserRule(int id = 0)
        {
            ViewBag.id = id;
            ViewBag.text = id == 0 ? "添加" : "修改";
            return View();
        }
        [HttpPost]
        public async Task<JsonResult> AddorEditUserRule(UserRuleInput input)
        {
            if (input.id == 0 && _systemSettingService.IsExistUserRule(input.title))
            {
                return Json(new { status = "n", info = "该特权已存在！" });
            }
            else
            {
                var result = await _systemSettingService.AddorEditUserRule(MemberId, input);
                if (result.code == 1)
                {
                    return Json(new { status = "y", info = "保存成功。" });
                }
                else
                {
                    return Json(new { status = "n", info = result.msg });
                }
            }
        }

        [HttpGet]
        public JsonResult GetUserRule(int id = 0)
        {
            if (id > 0)
            {
                var model = _systemSettingService.GetUserRuleById(id);
                return Json(model, JsonRequestBehavior.AllowGet);
            }
            return null;
        }

        public JsonResult GetUserRulePictures(int id)
        {
            var img_list = _systemSettingService.GetUserRulePictures(id);
            return Json(img_list);
        }

        public JsonResult UserRuleList(string sEcho, int iDisplayStart, int iDisplayLength, string name, int status)
        {
            int totalCount = 0;
            var list = _systemSettingService.GetAllUserRuleByPage(iDisplayStart, iDisplayLength, name, status, out totalCount);
            return Json(new { sEcho = sEcho, iTotalRecords = totalCount, iTotalDisplayRecords = totalCount, aaData = list }, JsonRequestBehavior.AllowGet);
        }

        //禁用/启用特权
        [HttpPost]
        public ActionResult SetUserRuleIsUse()
        {
            string id = Request.Form["id"];
            string isUse = Request.Form["isUse"];
            int idDefault = 0;
            int isUseDefault = 0;
            if (string.IsNullOrWhiteSpace(id) || !int.TryParse(id, out idDefault))
            {
                return Json(new { status = "n", info = "ID错误" });
            }
            if (string.IsNullOrWhiteSpace(isUse) || !int.TryParse(isUse, out isUseDefault))
            {
                return Json(new { status = "n", info = "参数错误" });
            }

            var result = _systemSettingService.SetIsUserRuleUse(MemberId, idDefault, isUseDefault);
            if (result)
            {
                return Json(new { status = "y", info = "操作成功。" });
            }
            else
            {
                return Json(new { status = "n", info = "操作失败。" });
            }
        }
        #endregion
    }
}